package com.ice_col.ui.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Toast;

import com.ice_col.ui.R;

/**
 * Created by Administrator on 2015/9/23 0023.
 */
public class Arcmenu extends ViewGroup implements View.OnClickListener {

    public static final int POS_LEFT_TOP = 0;
    public static final int POS_RIGHT_TOP = 1;
    public static final int POS_RIGHT_BOTTOM = 2;
    public static final int POS_LEFT_BOTTOM = 3;

    public enum Position {
        LEFT_TOP, RIGHT_TOP, RIGHT_BOTTOM, LEFT_BOTTOM
    }

    private int mRadius = 0;
    private Position mPostion = Position.RIGHT_BOTTOM;
    private View mCenterButton;
    private boolean isOpen = false;
    private OnMenuItemClickListener mOnMenuItemClickListener;

    public interface OnMenuItemClickListener {
        public void onClick();
    }

    public void setOnMenuItemClickListener(
            OnMenuItemClickListener mOnMenuItemClickListener) {
        this.mOnMenuItemClickListener = mOnMenuItemClickListener;
    }

    public Arcmenu(Context context) {
        super(context);
    }

    public Arcmenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public Arcmenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100,
                getResources().getDisplayMetrics());

        System.out.println("R1:" + mRadius);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Arcmenu, defStyleAttr, 0);
        int pos = a.getInt(R.styleable.Arcmenu_position, POS_RIGHT_BOTTOM);
        switch (pos) {
            case POS_LEFT_TOP:
                mPostion = Position.LEFT_TOP;
                break;
            case POS_RIGHT_TOP:
                mPostion = Position.RIGHT_TOP;
                break;
            case POS_RIGHT_BOTTOM:
                mPostion = Position.RIGHT_BOTTOM;
                break;
            case POS_LEFT_BOTTOM:
                mPostion = Position.LEFT_BOTTOM;
                break;
        }
        mRadius = (int) a.getDimension(R.styleable.Arcmenu_radius, mRadius);

        System.out.println("R2:" + mRadius);
        a.recycle();

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (changed) {
            layoutCenterButton();
            layoutMenuItems();
            mCenterButton.setOnClickListener(this);
        }
    }

    private void layoutCenterButton() {
        if (getChildCount() == 0) {
            return;
        }
        mCenterButton = getChildAt(0);
        int l = 0, t = 0;
        int width = mCenterButton.getMeasuredWidth();
        int height = mCenterButton.getMeasuredHeight();
        switch (mPostion) {
            case LEFT_TOP:
                l = t = 0;
                break;
            case LEFT_BOTTOM:
                l = 0;
                t = getMeasuredHeight() - height;
                break;
            case RIGHT_BOTTOM:
                l = getMeasuredWidth() - width;
                t = getMeasuredHeight() - height;
                break;
            case RIGHT_TOP:
                l = getMeasuredWidth() - width;
                t = 0;
                break;
        }
        mCenterButton.layout(l, t, l + width, t + height);
    }

    private void layoutMenuItems() {
        int itemCount = getChildCount() - 1;//默认第一个为中间的按钮
        if (itemCount < 1) {
            return;
        } else if (itemCount == 1) {
            View item = getChildAt(1);
            int l = 0, t = 0;
            int width = item.getMeasuredWidth();
            int height = item.getMeasuredHeight();
            System.out.println("width:" + width + "height:" + height);
            switch (mPostion) {
                case LEFT_TOP:
                    l = mRadius / 2;
                    t = mRadius / 2;
                    break;
                case LEFT_BOTTOM:
                    l = mRadius;
                    t = getMeasuredHeight() - mRadius / 2 - height;
                    break;
                case RIGHT_TOP:
                    l = getMeasuredWidth() - mRadius / 2 - width;
                    t = mRadius / 2;
                    break;
                case RIGHT_BOTTOM:
                    l = getMeasuredWidth() - mRadius / 2 - width;
                    t = getMeasuredHeight() - mRadius / 2 - height;
                    break;
            }
            item.layout(l, t, l + width, t + height);
        } else {
            for (int i = 1; i < itemCount + 1; i++) {
                View item = getChildAt(i);
                int l = 0, t = 0;
                int width = item.getMeasuredWidth();
                int height = item.getMeasuredHeight();
                double offsetX = 0, offsetY = 0;
                offsetX = mRadius * Math.sin((Math.PI / (2 * (itemCount - 1))) * (i - 1));
                offsetY = mRadius * Math.cos((Math.PI / (2 * (itemCount - 1))) * (i - 1));
                if (mPostion == Position.LEFT_BOTTOM || mPostion == Position.RIGHT_BOTTOM) {
                    t = (int) (getMeasuredHeight() - height - offsetY);
                }
                if (mPostion == Position.RIGHT_BOTTOM || mPostion == Position.RIGHT_TOP) {
                    l = (int) (getMeasuredWidth() - width - offsetX);
                }

                item.layout(l, t, l + width, t + height);
            }
        }
    }

    @Override
    public void onClick(View v) {
        rotateCbutton(v, 0f, 360f, 300);
        toggleItem(3000);
    }

    public void rotateCbutton(View v, float start, float end, int duration) {
        RotateAnimation rotateAnimation = new RotateAnimation(start, end,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(duration);
        rotateAnimation.setFillAfter(true);
        v.startAnimation(rotateAnimation);

    }

    public void toggleItem(long duration) {

        //平移动画：

        AnimationSet animationSet=new AnimationSet(true);

        Animation tranAnim=null;
        int count=getChildCount();
        for (int i = 0; i < count-1; i++) {
            final View view=getChildAt(i+1);
            view.setVisibility(VISIBLE);


            int cl=(int)(mRadius*Math.sin(Math.PI/2/(count-2)*i));
            int ct=(int) (mRadius*Math.cos(Math.PI/2/(count-2)*i));

            int xflag=1;
            int yflag=1;
            if(mPostion==Position.LEFT_TOP||mPostion==Position.LEFT_BOTTOM){
                xflag=-1;
            }
            if(mPostion==Position.LEFT_TOP||mPostion==Position.RIGHT_TOP){
                yflag=-1;
            }

           /* AnimationSet animationSet=new AnimationSet(true);

            Animation tranAnim=null;*/

            if(!isOpen){
                tranAnim=new TranslateAnimation(xflag*cl,0,yflag*ct,0);
                view.setClickable(true);
                view.setFocusable(true);
            }else{
                tranAnim=new TranslateAnimation(0,xflag*cl,0,yflag*ct);
                view.setClickable(false);
                view.setFocusable(false);
            }
            tranAnim.setFillAfter(true);
            tranAnim.setDuration(duration);

            tranAnim.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    if (!isOpen) {
                        view.setVisibility(GONE);
                    }
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            RotateAnimation rotateAnimation = new RotateAnimation(0f, 720f,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimation.setFillAfter(true);
            rotateAnimation.setDuration(duration);

            animationSet.addAnimation(rotateAnimation);
            animationSet.addAnimation(tranAnim);
            view.setAnimation(animationSet);
        }

        changeStatus();
    }

    private void changeStatus() {
        isOpen = isOpen ? false : true;
    }

}
